﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    static ClassExecute.ClassExecute dt;
    static string str = webapp4U.Configuration.CurrentConnectionString;
    static string error = "";
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod(EnableSession = true)]
    public bool KillSession()
    {
        HttpContext.Current.Session.RemoveAll();
        HttpContext.Current.Session.Abandon();
        if (Session["ShoppingCart"] != null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    [WebMethod]
    public void SendMail(string email,string name,string body)
    {
         
          SmtpClient smtp = new SmtpClient();
          MailMessage msg = new MailMessage(email,"khoi.nguyenhongmanh@gmail.com","Hỏi và Đáp",body);
          msg.IsBodyHtml = true;
          smtp.Host = "smtp.gmail.com";//Sử dụng SMTP của gmail
          NetworkCredential mailAuthentication = new NetworkCredential("khoi.nguyenhongmanh@gmail.com", "sn190685");
          smtp.EnableSsl = true;
          smtp.UseDefaultCredentials = false;
          smtp.Credentials = mailAuthentication;
          smtp.Send(msg); 

    }
    [WebMethod(EnableSession=true)]
    public string CheckAccount(string uid, string pwd)
    {
        string kq = "";
        dt = new ClassExecute.ClassExecute();
        string Md5 = dt.GetMd5Sum(pwd);

        kq = dt.GetDataDepend(str, "k_CheckAccountBA", CommandType.StoredProcedure, ref error,
            new SqlParameter("@UserName", uid),
            new SqlParameter("@Password", Md5));
        if (kq != "Null")
        {
            if (HttpContext.Current.Session["sender"] != null)
            {
                HttpContext.Current.Session["sender"] = null;
            }
            HttpContext.Current.Session["TypeID"] = kq;
            HttpContext.Current.Session["UserName"] = uid;
            HttpContext.Current.Session["masterpage"] = "~/MasterPage/Common.master";
        }
        return kq;
    }
    [WebMethod(EnableSession = true)]
    public string GetSlogan()
    {
        string kq = "";
        dt = new ClassExecute.ClassExecute();
        kq = dt.GetDataDepend(str, "k_getSlogan", CommandType.StoredProcedure, ref error,
            new SqlParameter("@salesp_key", HttpContext.Current.Session["UserName"].ToString()));
        return kq;

    }
    [WebMethod]
    public ArrayList GetProvince()
    {

        ArrayList listTinhThanh = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[GET_DATA_PROVINCE]", CommandType.StoredProcedure);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            listTinhThanh.Add(new { Value = db.Rows[i][0].ToString(), Display = db.Rows[i][1].ToString() });

        }
        return listTinhThanh;
    }
    [WebMethod]
    public ArrayList GetTown(string Province)
    {
        ArrayList listTown = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db1 = dt.Get_Data_DataTable(str, ref error, "[GET_DATA_DISTRICTOWN_PROVINCECODE]", CommandType.StoredProcedure,
            new SqlParameter("@ProvinceCode", Province));

        for (int i = 0; i < db1.Rows.Count; i++)
        {
            listTown.Add(new { Value = db1.Rows[i][0].ToString(), Display = db1.Rows[i][1].ToString() });

        }
        return listTown;

    }
    [WebMethod]
    public string CheckIDNo(string IDNo, string salesp_key)
    {
        dt = new ClassExecute.ClassExecute();
        int kq = 1;
        kq = dt.ExecuteSQl_int(str, "ls_isDupIDNo", CommandType.StoredProcedure, ref error,
            new SqlParameter("@IDNo", IDNo),
            new SqlParameter("@salespKey", salesp_key));
        return kq.ToString();
    }
    [WebMethod]
    public string CheckTelNo(string TelNo, string salesp_key)
    {
        dt = new ClassExecute.ClassExecute();
        int kq = 1;
        kq = dt.ExecuteSQl_int(str, "ls_isDupTelNo", CommandType.StoredProcedure, ref error,
            new SqlParameter("@TelNo", TelNo),
            new SqlParameter("@salespKey", salesp_key));
        return kq.ToString();
    }
    [WebMethod]
    public ArrayList  GET_SALESPNAME(string salesp_key)
    {
       
        ArrayList Level = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db1 = dt.Get_Data_DataTable(str, ref error, "GET_SALESPNAME", CommandType.StoredProcedure,
            new SqlParameter("@salesp_key", salesp_key));
        for (int i = 0; i < db1.Rows.Count; i++)
        {
            Level.Add(new { Value = db1.Rows[i][0].ToString(), Display = db1.Rows[i][1].ToString() });

        }
        return Level;
        
    }
    //Đăng ký thành viên
    [WebMethod(EnableSession = true)]
    public string RegisterNPP(string salesPName, string birthDay, string sex, string rootAddress, string IDNo, string recommender, string address, string street, string province, string town, string mobile)
    {
         dt = new ClassExecute.ClassExecute();
       
        string kq = "";
        bool ConvertSex = false;
        if (sex == "1")
        {
            ConvertSex = true;
        }

       
        if (recommender == "option")
        {
            if (HttpContext.Current.Session["sender"] != null)
            {
                recommender = HttpContext.Current.Session["sender"].ToString();
            }
            else
            {
                recommender = dt.GetDataDepend(str, "[ls_getBASupport]", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@IDNo", IDNo),
                    new SqlParameter("@Phone", mobile),
                    new SqlParameter("@ProvinceID", province),
                    new SqlParameter("@TownID", town));
            }
        }
        kq = dt.GetDataDepend(str, "[ls_saveOnlineSalesPersonInfo]", CommandType.StoredProcedure, ref error,
            new SqlParameter("@salesPName", salesPName),
            new SqlParameter("@birthDay", birthDay),
            new SqlParameter("@sex", ConvertSex),
            new SqlParameter("@rootAddress", rootAddress),
            new SqlParameter("@IDNo", IDNo),
            new SqlParameter("@recommender", recommender),
            new SqlParameter("@address", address),
            new SqlParameter("@street", street),
            new SqlParameter("@province", province),
            new SqlParameter("@town", town),
            new SqlParameter("@mobile", mobile));

        //Cập nhật MD5 cho password
        string PassMD5 = dt.GetMd5Sum(kq);
        bool XuLy = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "K_updatePassToMd5", ref error,
            new SqlParameter("@salesp_key", kq),
            new SqlParameter("@Md5", PassMD5));
        if (HttpContext.Current.Session["UserName"] == null)
        {
           string kq1= CheckAccount(kq, kq);
           
        }
       
        return kq;
    }
    //xác định Recommender khi vào màn hình đăng ký thành viên
    [WebMethod(EnableSession = true)]
    public string ConfirmSessionSender()
    {
        if (HttpContext.Current.Session["sender"] != null)
        {
            return HttpContext.Current.Session["sender"].ToString();
        }
        else if (HttpContext.Current.Session["UserName"] != null)
        {
            return HttpContext.Current.Session["UserName"].ToString();
        }
        else
        {
            return "notreadonly";
        }
    }
    //Gửi mã random khi đăng ký thành viên
    [WebMethod]
    public string GetRandom(string TelNo)
    {
        dt = new ClassExecute.ClassExecute();
        string kq = dt.GetDataDepend(str, "k_MakeRanDomNo", CommandType.StoredProcedure, ref error,
            new SqlParameter("@TelNo", TelNo));
        return kq;
    }
    [WebMethod(EnableSession = true)]
    public string MakeLink()
    {
        return "http://bizsolutions.vn/Page/ThongTinChung/Default.aspx?sender=" + HttpContext.Current.Session["UserName"].ToString();
    }
    [WebMethod(EnableSession = true)]
    public ArrayList GetUserAndPass()
    {
        ArrayList Level = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db1 = dt.Get_Data_DataTable(str, ref error, "k_getPass", CommandType.StoredProcedure,
            new SqlParameter("@salesp_key", HttpContext.Current.Session["UserName"].ToString()));
        for (int i = 0; i < db1.Rows.Count; i++)
        {
            Level.Add(new { Value = db1.Rows[i][0].ToString(), Display = db1.Rows[i][1].ToString() });

        }
        return Level;
    }
     [WebMethod(EnableSession = true)]
    public string ChangePass(string PW)
    {
        dt = new ClassExecute.ClassExecute();
        string Md5 = dt.GetMd5Sum(PW);
        bool kq=dt.ExecuteSQl_bool(str,CommandType.StoredProcedure,"K_ChangePass",ref error,
            new SqlParameter("@salesp_key",HttpContext.Current.Session["UserName"].ToString()),
            new SqlParameter("@PW",PW),
            new SqlParameter("@Md5",Md5));
        if (kq)
        {
            return "Bạn đã thay đổi mật khẩu thành công";
        }
        else
        {
            return "Bạn đã thay đổi mật khẩu thất bại";
        }
    }
    // Code that runs when an unhandled error occurs

    //WebService.Errors.ErrorHandler.LogError(HttpContext.Current, Server.GetLastError(), "NOLDFHOI");
}
